package com.wlcorp.forcazamis.business.system.impl;

import java.util.List;

import com.wlcorp.forcazamis.business.system.IStaffService;
import com.wlcorp.forcazamis.dao.baseinfo.IDepotDAO;
import com.wlcorp.forcazamis.dao.system.IDepartmentDAO;
import com.wlcorp.forcazamis.dao.system.IRoleDAO;
import com.wlcorp.forcazamis.dao.system.IStaffDAO;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.exception.ServiceException;
import com.wlcorp.forcazamis.sys.BaseService;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.LoginSecurity;

public class StaffServiceImpl extends BaseService implements IStaffService {
	
	private IStaffDAO staffDAO;
	private IRoleDAO roleDAO;
	private IDepartmentDAO departmentDAO;
	private IDepotDAO depotDAO;
	public List<Staff> getStaffList(Staff staff) throws ServiceException {
		return staffDAO.getStaffList(staff);
	}

	public Staff getStaffById(Long staffId) throws ServiceException {
		if(null == staffId){
			return null;
		}
		Staff staff = staffDAO.getStaffById(staffId);
		staff.setTmpPassword(LoginSecurity.decryptStr(staff.getPassword()));
		return staff;
	}

	public void deleteStaff(Staff staff) throws ServiceException {
		staff.setFlag(AppConstant.RECORD_DELETED);
		staffDAO.saveStaff(staff);
	}

	public void saveStaff(Staff staff) throws ServiceException {
//		新增时，检验工号是否重复
		if(null == staff.getStaffId()){
			Staff tmpStaff = new Staff();
			tmpStaff.setStaffCode(staff.getStaffCode());
			Long count = staffDAO.getStaffCount(tmpStaff);
			if(count > 0){
				setErrorField(staff,AppConstant.BASE_BEAN_STATUS_ERROR,"工号重复，请重新输入！");
				return;
			}
		}
		if(null != staff.getDepartmentId()){
			staff.setDepartment(departmentDAO.getDepartmentById(staff.getDepartmentId()));
		}else{
			staff.setDepartment(null);
		}
		if(null != staff.getManagerId()){
			staff.setManager(staffDAO.getStaffById(staff.getManagerId()));
		}else{
			staff.setManager(null);
		}
		if(null != staff.getRoleId()){
			staff.setRole(roleDAO.getRoleById(staff.getRoleId()));
		}else{
			staff.setRole(null);
		}
		if(null != staff.getDepotId()){
			staff.setDepot(depotDAO.getDepotById(staff.getDepotId()));
		}else{
			staff.setDepot(null);
		}
		//密码加密
		staff.setPassword(LoginSecurity.encryptStr(staff.getTmpPassword()));
		staffDAO.saveStaff(staff);
	}
	
	public Staff getStaffByCodeAndPassword(String staffCode, String password) throws ServiceException{
		password = LoginSecurity.encryptStr(password);
		return staffDAO.getStaffByCodeAndPassword(staffCode,password);
	}
	
	public void setStaffDAO(IStaffDAO staffDAO) {
		this.staffDAO = staffDAO;
	}

	public void setDepartmentDAO(IDepartmentDAO departmentDAO) {
		this.departmentDAO = departmentDAO;
	}

	public void setRoleDAO(IRoleDAO roleDAO) {
		this.roleDAO = roleDAO;
	}

	public Long getStaffCount(Staff staff) throws ServiceException {
		return staffDAO.getStaffCount(staff);
	}

	public void setDepotDAO(IDepotDAO depotDAO) {
		this.depotDAO = depotDAO;
	}

	public void modifyStaffPassword(Staff staff) throws ServiceException {
//		密码加密
		staff.setPassword(LoginSecurity.encryptStr(staff.getTmpPassword()));
		staffDAO.saveStaff(staff);
	}
	
	
}
